package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录功能和
 * 操作员工的业务
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登陆功能
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        //1.判断用户名是否一样,
        //1.1获取用户名

        String username = employee.getUsername();

        //1.2第一步创建条件对象
        QueryWrapper<Employee> wrapper=new QueryWrapper();
        wrapper.eq("username",username);
        //1.3获取表里面的emp对象
        Employee emp = employeeService.getOne(wrapper);
        if (emp==null){
            //用户名不存在
            return R.error("用户名不存在");
        }

        //2.判断密码是否一样
        //获取客户端的密码进行d5加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!password.equals(emp.getPassword())){
            //密码错误
            return R.error("密码错误");
        }

        //3.判断启用状态是否为1
        if (emp.getStatus()==0){
            return R.error("该用户已被禁止");
        }
        //4.登录成功后,将数据存到session中去
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);
    }

    /**
     * 员工退出
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        //销毁session数据  invalidate 销毁的方法
        session.invalidate();
        return R.success("退出成功");
    }

    /**
     * 增加员工信息
     * @param employee
     * @return
     */

    @PostMapping
    public R<String> add(@RequestBody Employee employee, HttpSession session){
          //设置默认密码
          employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*  //设置操作人
          employee.setCreateUser((Long) session.getAttribute("employee"));
          employee.setUpdateUser((Long) session.getAttribute("employee"));*/

          //调用业务层的方法
          employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
      /*  //设置修改人
        employee.setUpdateUser((Long) session.getAttribute("employee"));*/
            employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    /**
     * 带条件分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> selectPage(String name,Integer page,Integer pageSize){
        Page<Employee> employeePage = employeeService.selectPage(name, page, pageSize);
        return R.success(employeePage);
    }
}
